﻿using UnityEngine;
using System.Collections;

public class OptionMenu {
	public  UISprite uiBackground;
	private UIHorizontalLayout uiHorLayout;
	private UIButton btnClose;
	private UISprite spVolumen;
	private UISlider sliCtrlVol;
	private OptionMenuViewModel mvOption;
	
	private void CreateControls()
	{
		uiBackground = UI.firstToolkit.addSprite("FondoMenu2b.png",0,0,2);
		GetBtnClose().parentUIObject = uiBackground;
		GetSpVolumen().parentUIObject= uiBackground;
		GetsliCtrlVol().parentUIObject = uiBackground;
		mvOption = new OptionMenuViewModel();
	}
	
	private void PositionControls()
	{
		uiBackground.positionCenter();
		GetBtnClose().positionFromTopRight(0.0f,0.0f);
		GetSpVolumen().positionFromLeft(0.1f);
		GetsliCtrlVol().positionFromLeft(0.3f);
		GetsliCtrlVol().refreshAnchorInformation();
		GetsliCtrlVol().centerize();
	}
	
	private void ResizeControls()
	{
	}
	
	private void PutActions()
	{
		GetBtnClose().onTouchUp += CloseMenu;
		GetsliCtrlVol().onChange += mvOption.ChangeVolume; 
	}

	private UIButton GetBtnClose()
	{
		if(btnClose == null){
			btnClose = UIButton.create("ExitMenuSmall.png","ExitMenuSmall.png",0,0);
		}
		return btnClose;
	}
	
	private UISprite GetSpVolumen()
	{
		if(spVolumen == null)
		{
			spVolumen = new UISprite();
			spVolumen= UI.firstToolkit.addSprite("Song.png",0,0,0);
		}
		return spVolumen;
	}
	
	private UISlider GetsliCtrlVol()
	{
		if(sliCtrlVol == null)
		{
			sliCtrlVol= UISlider.create("Knock.png", "SliderFondo.png", 0, 0, UISliderLayout.Horizontal );
			sliCtrlVol.value = 1.0f;
		}
		return sliCtrlVol;
	}
	
	public void Show()
	{
		if(uiBackground == null)
			CreateControls();
		else
			OpenMenu();	
		PositionControls();
		PutActions();
		
	}
	
	public bool GetStateShow()
	{
		if(uiBackground != null)
			return !uiBackground.hidden;
		return false;
	}
	
	public void OpenMenu()
	{
		GetBtnClose().hidden = false;
		GetSpVolumen().hidden = false;
		GetsliCtrlVol().hidden = false;
		uiBackground.hidden = false;
	}
	
	public void CloseMenu(UIButton sender)
	{
		GetBtnClose().hidden = true;
		GetSpVolumen().hidden = true;
		GetsliCtrlVol().hidden = true;
		uiBackground.hidden = true;
	}
	
}
